<?php

namespace plugin\webman\gateway;


use support\Redis;

class RedisHelper
{
    public function addFriend($sn, $from, $to)
    {
        $friends = Redis::hGetAll($sn . ':friend:' . $from);

        foreach ($friends as $friend) {
            $friend = json_decode($friend, true);
            if ($friend['name'] == $to) return;
        }

        $data = ['name' => $to, 'group' => false, 'unread' => 0];
        Redis::hSet($sn . ':friend:' . $from, $to, json_encode($data, JSON_UNESCAPED_UNICODE));

        $data['name'] = $from;
        Redis::hSet($sn . ':friend:' . $to, $from, json_encode($data, JSON_UNESCAPED_UNICODE));
    }

    public function getFriend($sn, $name)
    {
        //$friends = Redis::lrange($sn .':friend:'. $name, 0, 999);
        $friends = Redis::hGetAll($sn . ':friend:' . $name);
        $group   = Redis::hGetAll($sn . ':group');
        //$group = $group ? json_decode($group, true) : [];
        //$group = array_column($group,NULL,'name');

        foreach ($friends as &$friend) {
            $friend = json_decode($friend, true);
            if ($friend['group']) {
                $friend['members'] = json_decode($group[$friend['name']], true);
            }
        }

        return $friends;
    }

    public function searchUser($sn, $name)
    {
        $data = Redis::hGet($sn . ':user', $name);
    }

    public function userList($sn)
    {
        $data = Redis::hGetAll($sn . ':user');
        foreach ($data as $key => &$val) {
            $val = json_decode($val, true);
            unset($val['passwd']);

            if ($val['name'] === 'admin') {
                unset($data[$key]);
            }
        }
        return $data;
    }

    public function getUser($sn, $data, $add = false)
    {
        if ($add) {
            $data = [
                'id'     => $data['id'],
                'name'   => $data['name'],
                'passwd' => $data['passwd'],
                'img'    => $data['img'],
            ];

            return Redis::hSet($sn . ':user', $data['name'], json_encode($data, JSON_UNESCAPED_UNICODE));
        }

        $data = Redis::hGet($sn . ':user', $data['name']);
        return json_decode($data, true);
    }

    public function saveMsg($sn, $name, $msg)
    {
        return Redis::rpush($sn . ':record:' . $name, $msg);
    }

    public function getMsg($sn, $name)
    {
        $msg = Redis::lRange($sn . ':record:' . $name, 0, 999);
        foreach ($msg as &$val) {
            $val = json_decode($val, true);
        }

        return $msg;
    }

    public function getGroupMember($sn, $name)
    {
        $users = Redis::hGet($sn . ':group', $name);
        return json_decode($users, true);
    }

    public function updateUnread($sn, $data, $read = false)
    {
        $friend  = Redis::hGet($sn . ':friend:' . $data['to'], $data['from']);
        $newChat = 0;
        if ($read) {
            $friend           = json_decode($friend, true);
            $friend['unread'] = 0;
        } elseif (!empty($friend)) {
            $friend              = json_decode($friend, true);
            $friend['unread']    += 1;
            $friend['last_time'] = $data['time'];
        } else {
            // 双向加好友
            Redis::hSet($sn . ':friend:' . $data['from'], $data['to'], json_encode([
                'group'     => false,
                'name'      => $data['to'],
                'unread'    => 0,
                'last_time' => $data['time']
            ], JSON_UNESCAPED_UNICODE));

            $friend  = [
                'group'     => false,
                'name'      => $data['from'],
                'unread'    => 1,
                'last_time' => $data['time']
            ];
            $newChat = 1;
        }

        Redis::hSet($sn . ':friend:' . $data['to'], $data['from'], json_encode($friend, JSON_UNESCAPED_UNICODE));
        return $newChat;
    }

    public function updateUser($sn, $data)
    {
        if (!empty($data['del'])) {
            return Redis::hdel($sn . ':user', $data['name']);
        }
        $user = Redis::hGet($sn . ':user', $data['name']);
        $data = [
            'name'   => $data['name'],
            'passwd' => $data['passwd'],
        ];
        return Redis::hSet($sn . ':user', $data['name'], json_encode($data, JSON_UNESCAPED_UNICODE));
    }

    public function joinGroup($sn, $groupName, $data)
    {
        $res   = Redis::hExists($sn . ':group', $groupName);
        $_data = [];
        $json  = json_encode(['group' => true, 'name' => $groupName, 'unread' => 0, 'last_time' => time()], JSON_UNESCAPED_UNICODE);
        if ($res) {
            $_data = json_decode(Redis::hGet($sn . ':group', $groupName), true);
            $_name = array_column($_data, 'name');
            $name  = array_diff($data, $_name);
            foreach ($name as $val) {
                $_data[] = ['name' => $val];

                // 聊天列表中
                Redis::hSet($sn . ':friend:' . $val, $groupName, $json);
            }
        } else {
            foreach ($data as $name) {
                $_data[] = ['name' => $name];
                Redis::hSet($sn . ':friend:' . $name, $groupName, $json);
            }
        }
        Redis::hSet($sn . ':group', $groupName, json_encode($_data, JSON_UNESCAPED_UNICODE));
        return $_data;
    }

    public function leaveGroup($sn, $data)
    {
        $group = Redis::hGet($sn . ':group', $data['group_name']);
        $group = json_decode($group, true);

        foreach ($group as $key => $member) {
            if ($member['name'] = $data['from']) {
                unset($group[$key]);
                break;
            }
        }
        $group = array_values($group);

        Redis::hdel($sn . ':friend:' . $data['from'], $data['group_name']);

        if (empty($group)) {
            Redis::hdel($sn . ':group', $data['group_name']);
        } else {
            Redis::hSet($sn . ':group', $data['group_name'], json_encode($group, JSON_UNESCAPED_UNICODE));
        }

        return $group;
    }

    public function delFriend($sn, $data)
    {
        Redis::hdel($sn . ':friend:' . $data['from'], $data['name']);
    }

    public function updateLasttime($sn, $data)
    {
        $friend              = Redis::hGet($sn . ':friend:' . $data['from'], $data['name']);
        $friend              = json_decode($friend, true);
        $friend['last_time'] = $data['time'];

        Redis::hSet($sn . ':friend:' . $data['from'], $data['name'], json_encode($friend, JSON_UNESCAPED_UNICODE));
    }
}